<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: IndexController.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_PackageController extends Core_Controller_Action_Standard {
	
	//ACTION FOR SHOW PACKAGES
	public function indexAction() {
		
		//USER VALIDATON
		if (! $this->_helper->requireUser ()->isValid ())
			return;
		
		//PAGE CREATION PRIVACY
		if (! $this->_helper->requireAuth ()->setAuthParams ( 'sitepage_page', null, 'create' )->isValid ())
			return;
		
		if (! Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
			//REDIRECT
			return $this->_helper->redirector->gotoRoute ( array ('action' => 'create' ), 'sitepage_general', true );
		}
		$is_package_view = Zend_Registry::isRegistered ( 'sitepage_package_view' ) ? Zend_Registry::get ( 'sitepage_package_view' ) : null;
		
		//GET NAVIGATION
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_main' );
		
		$packages_select = Engine_Api::_ ()->getItemtable ( 'sitepage_package' )->getPackagesSql ();
		$paginator = Zend_Paginator::factory ( $packages_select );
		if (empty ( $is_package_view )) {
			$this->view->paginator = array ();
		} else {
			$this->view->paginator = $paginator->setCurrentPageNumber ( $this->_getParam ( 'page' ) );
		}
	}
	
	//ACTION FOR PACKAGE DETAIL
	public function detailAction() {
		
		//USER VALIDATON
		if (! $this->_helper->requireUser ()->isValid ())
			return;
		
		//PAGE CREATION PRIVACY
		if (! $this->_helper->requireAuth ()->setAuthParams ( 'sitepage_page', null, 'create' )->isValid ())
			return;
		
		$id = $this->_getParam ( 'id' );
		if (empty ( $id )) {
			return $this->_forward ( 'notfound', 'error', 'core' );
		}
		$this->view->package = Engine_Api::_ ()->getItem ( 'sitepage_package', $id );
	}
	
	//ACTION FOR PACKAGE UPDATION
	public function updatePackageAction() {
		
		//USER VALIDATON
		if (! $this->_helper->requireUser ()->isValid ())
			return;
		
		//PACKAGE ENABLE VALIDATION
		if (! Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
			return $this->_forward ( 'notfound', 'error', 'core' );
		}
		
		$this->view->sitepages_view_menu = 15;
		
		//GET PAGE ID PAGE OBJECT AND THEN CHECK VALIDATIONS
		$this->view->page_id = $page_id = $this->_getParam ( 'page_id' );
		if (empty ( $page_id )) {
			return $this->_forward ( 'notfound', 'error', 'core' );
		}
		$this->view->sitepage = $sitepage = Engine_Api::_ ()->getItem ( 'sitepage_page', $page_id );
		if (empty ( $sitepage )) {
			return $this->_forward ( 'notfound', 'error', 'core' );
		}
		
		//START MANAGE-ADMIN CHECK
		$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'edit' );
		if (empty ( $isManageAdmin )) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		//END MANAGE-ADMIN CHECK
		

		$this->view->package = Engine_Api::_ ()->getItem ( 'sitepage_package', $sitepage->package_id );
		$table = Engine_Api::_ ()->getItemtable ( 'sitepage_package' );
		$packages_select = $table->getPackagesSql ()->where ( "update_list = ?", 1 )->where ( "enabled = ?", 1 )->where ( "package_id <> ?", $sitepage->package_id );
		$paginator = Zend_Paginator::factory ( $packages_select );
		
		$this->view->paginator = $paginator->setCurrentPageNumber ( $this->_getParam ( 'page' ) );
		
		//GET NAVIGATION
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_main' );
		$this->view->is_ajax = $this->_getParam ( 'is_ajax', '' );
	}
	
	//ACTION FOR PACKAGE UPGRADE CONFIRMATION
	public function updateConfirmationAction() {
		
		//USER VALIDATION
		if (! $this->_helper->requireUser ()->isValid ())
			return;
		
		//PACKAGE ENABLE VALIDATION
		if (! Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
			return $this->_forward ( 'notfound', 'error', 'core' );
		}
		
		//GET PAGE ID PAGE OBJECT AND THEN CHECK VALIDATIONS
		$this->view->page_id = $page_id = $this->_getParam ( 'page_id' );
		if (empty ( $page_id )) {
			return $this->_forward ( 'notfound', 'error', 'core' );
		}
		$this->view->sitepage = $sitepage = Engine_Api::_ ()->getItem ( 'sitepage_page', $page_id );
		if (empty ( $sitepage )) {
			return $this->_forward ( 'notfound', 'error', 'core' );
		}
		
		//START MANAGE-ADMIN CHECK
		$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'edit' );
		if (empty ( $isManageAdmin )) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		//END MANAGE-ADMIN CHECK
		

		$this->view->package_id = $this->_getParam ( 'package_id' );
		$package_chnage = Engine_Api::_ ()->getItem ( 'sitepage_package', $this->view->package_id );
		if (empty ( $package_chnage ) || ! $package_chnage->enabled) {
			return $this->_forward ( 'notfound', 'error', 'core' );
		}
		$getPackageAuth = Engine_Api::_ ()->sitepage ()->getPackageAuthInfo ( 'sitepage' );
		
		if ($this->getRequest ()->getPost ()) {
			
			if (! empty ( $_POST ['package_id'] )) {
				$table = $sitepage->getTable ();
				$db = $table->getAdapter ();
				$db->beginTransaction ();
				
				try {
					
					$sitepage->package_id = $_POST ['package_id'];
					$package = Engine_Api::_ ()->getItem ( 'sitepage_package', $sitepage->package_id );
					
					$sitepage->featured = $package->featured;
					$sitepage->sponsored = $package->sponsored;
					$sitepage->pending = 1;
					$sitepage->expiration_date = new Zend_Db_Expr ( 'NULL' );
					$sitepage->status = 'initial';
					if (($package->isFree ()) && ! empty ( $getPackageAuth )) {
						$sitepage->approved = $package->approved;
					} else {
						$sitepage->approved = 0;
					}
					
					if (! empty ( $sitepage->approved )) {
						$sitepage->pending = 0;
						$expirationDate = $package->getExpirationDate ();
						if (! empty ( $expirationDate ))
							$sitepage->expiration_date = date ( 'Y-m-d H:i:s', $expirationDate );
						else
							$sitepage->expiration_date = '2250-01-01 00:00:00';
						
						if (empty ( $sitepage->aprrove_date )) {
							$sitepage->aprrove_date = date ( 'Y-m-d H:i:s' );
							if (! empty ( $sitepage->draft ) && empty ( $sitepage->pending )) {
								Engine_Api::_ ()->sitepage ()->attachPageActivity ( $sitepage );
							}
						}
					}
					$sitepage->save ();
					$db->commit ();
				} catch ( Exception $e ) {
					$db->rollBack ();
					throw $e;
				}
			}
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'format' => 'smoothbox', 'parentRedirect' => $this->view->url ( array ('action' => 'update-package', 'page_id' => $sitepage->page_id ), 'sitepage_packages', true ), 'parentRedirectTime' => 15, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'The package for your Page has been successfully changed.' ) ) ) );
		}
	}
	
	//ACTION FOR PACKAGE PAYMENT
	public function paymentAction() {
		
		//USER VALIDATION
		if (! $this->_helper->requireUser ()->isValid ())
			return;
		
		//GET PAGE ID PAGE OBJECT AND THEN CHECK VALIDATIONS
		$page_id = $_POST ['page_id_session'];
		if (empty ( $page_id )) {
			return $this->_forward ( 'notfound', 'error', 'core' );
		}
		$this->view->sitepage = $sitepage = Engine_Api::_ ()->getItem ( 'sitepage_page', $page_id );
		if (empty ( $sitepage )) {
			return $this->_forward ( 'notfound', 'error', 'core' );
		}
		
		$getPackageAuth = Engine_Api::_ ()->sitepage ()->getPackageAuthInfo ( 'sitepage' );
		$package = Engine_Api::_ ()->getItem ( 'sitepage_package', $sitepage->package_id );
		
		if ((! $package->isFree ()) && ! empty ( $getPackageAuth )) {
			$session = new Zend_Session_Namespace ( 'Payment_Sitepage' );
			$session->page_id = $page_id;
			
			return $this->_helper->redirector->gotoRoute ( array (), 'sitepage_payment', true );
		} else {
			return $this->_helper->redirector->gotoRoute ( array ('action' => 'manage' ), 'sitepage_general', true );
		}
	}
}
?>